/**
 * 
 */
package net.mikoo.seals.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * @author huangxm
 * 
 */
public class DateUtil {
	
	
	
	public static String getCurrentStr(String fromate) {

		SimpleDateFormat sf = new SimpleDateFormat(fromate);
		return sf.format(new Date());
	}

	public static String addMinute(String date, int minute, String fromate) throws ParseException {

		SimpleDateFormat sf = new SimpleDateFormat(fromate);
		Date dates = sf.parse(date);

		Calendar nowTime = Calendar.getInstance();
		nowTime.setTime(dates);
		nowTime.add(Calendar.MINUTE, minute);

		return sf.format(nowTime.getTime());
	}

	/**
	 * 
	 * @return
	 */
	public static Date nextYear() {

		Calendar curr = Calendar.getInstance();
		curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + 1);
		Date date = curr.getTime();
		return date;
	}
	
	public static String formate(Date date,String formate){
		if(date == null){
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formate);
		return sdf.format(date);
	}
	
	public static Date stringToDate(String date,String formate){
		
		if(StringUtils.isBlank(date)){
			return null;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(formate);
		Date d = null;
		try {
			d = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	
	public static Date getCurretDate(){
		
		
		return new Date();
	}
	
	public static Date addMinute(Date date,int num){
		
		Calendar nowTime = Calendar.getInstance();
		nowTime.setTime(date);
		nowTime.add(Calendar.MINUTE, num);
		return nowTime.getTime();
	}
	
	public static Date addMonth(Date date,int num){
		
		Calendar nowTime = Calendar.getInstance();
		nowTime.setTime(date);
		nowTime.add(Calendar.MONTH, num);
		return nowTime.getTime();
	}
	
	/**
	 * 
	 *	比较日期大小
	 *	date1 > date2  为1
	 *	date1 = date2  为0
	 *	date1 < date2  为-1
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compare(Date date1, Date date2) {

		try {

			if (date1.getTime() > date2.getTime()) {
				return 1;
			} else if (date1.getTime() < date2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}
	
	
	/**
	 * 显示时间，如果与当前时间差别小于一天，则自动用**秒(分，小时)前，如果大于一天则用format规定的格式显示
	 * 
	 * @author wxy
	 * @param ctime
	 *            时间
	 * @param format
	 *            格式 格式描述:例如:yyyy-MM-dd yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String showTimeStyle(Date ctime, String format) {
		// System.out.println("当前时间是："+new Timestamp(System.currentTimeMillis()));

		// System.out.println("发布时间是："+df.format(ctime).toString());
		String r = "";
		if (ctime == null)
			return r;
		if (format == null)
			format = "MM-dd HH:mm";

		long nowtimelong = System.currentTimeMillis();

		long ctimelong = ctime.getTime();
		long result = Math.abs(nowtimelong - ctimelong);

		if (result < 60000) {// 一分钟内
			long seconds = result / 1000;
			if (seconds == 0) {
				r = "刚刚";
			} else {
				r = seconds + "秒前";
			}
		} else if (result >= 60000 && result < 3600000) {// 一小时内
			long seconds = result / 60000;
			r = seconds + "分钟前";
		} else if (result >= 3600000 && result < 86400000) {// 一天内
			long seconds = result / 3600000;
			r = seconds + "小时前";
		} else if (result >= 86400000 && result < 1702967296) {// 三十天内
			long seconds = result / 86400000;
			r = seconds + "天前";
		} else {// 日期格式
			format = "MM-dd HH:mm";
			SimpleDateFormat df = new SimpleDateFormat(format);
			r = df.format(ctime).toString();
		}
		return r;
	}

	public static void main(String[] args) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			System.out.println(showTimeStyle(df.parse("2017-08-11 19:31:00"), "yyyy-MM-dd HH:mm:ss"));
		} catch (Exception e) {
			// TODO: handle exception
		}
}


}
